package top.huhuiyu.javabasic.javabasictemplate;

import top.huhuiyu.javabasic.javabasictemplate.dao.TbNewsDAO;
import top.huhuiyu.javabasic.javabasictemplate.entity.CommonInfo;
import top.huhuiyu.javabasic.javabasictemplate.entity.Page;
import top.huhuiyu.javabasic.javabasictemplate.entity.TbNews;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet(urlPatterns = "/news/list.action")
public class QueryNewsServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doPost(req, resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        TbNews news = new TbNews();
        news.setTitle(req.getParameter("title"));
        news.setSource(req.getParameter("source"));
        Page page = new Page();
        try {
            if (req.getParameter("pageNumber") != null) {
                page.setPageNumber(Integer.parseInt(req.getParameter("pageNumber")));
            }

            if (req.getParameter("pageSize") != null) {
                page.setPageSize(Integer.parseInt(req.getParameter("pageSize")));
            }
        } catch (Exception ex) {
        }
        System.out.println("查询条件：" + news);
        System.out.println("分页信息：" + page);
        try {
            List<TbNews> list = TbNewsDAO.query(news, page);
            req.setAttribute("list", list);
            req.setAttribute("page", page);
            req.setAttribute("query", news);
            req.getRequestDispatcher("/news.jsp").forward(req, resp);
        } catch (SQLException e) {
            throw new ServletException(e);
        }
    }
}
